<?php
class LoginAction extends Action{

		function index(){
			$this->display();
		}
		
		function checkLogin(){

			$user=trim($this->_post('username'));;
			$pass=trim($this->_post('password'));
			$verify=trim($this->_post('verify'));

			//判断登陆用户
			if(!$verify){$this->error('验证码不能为空');}
			if(!$user){$this->error('用户名不能为空');}
			if(!$pass){$this->error('密码不能为空');}

			if(md5($verify)!==$_SESSION['verify']){
				$this->error('对不起！您输入的验证码不正确！');
			}

			$admin=M('Admin');
			$where['username']=$user;
			$res=$admin->where($where)->find();
			if( ($res['errornum']+1) >5){
				$this->error('你的信息输入错误次数过多，1小时内禁止登陆！');
			}

			if(!$res || $res['status']!=='1'){
				$this->error('帐号不存在或已被禁用！');
			}else{
				if(md5($pass.$res['x'])!==$res['password']){

					if(!$_SESSION['admin']['time']){
						$_SESSION['admin']['time']=time();
					}
					if(  ( time() - $_SESSION['admin']['time'] ) < 3600 ){
						$data['errornum'] = array('exp','errornum+1');	//错误次数+1
						$admin->where($where)->save($data);
					}else{
						$_SESSION['admin']['time']=time();
						$data['errornum'] = 1;
						$admin->where($where)->save($data);				
					}
					$this->error('您输入的帐号信息不正确，请重新输入！');
				}else{
					//提取要替换的数据
					unset($_SESSION['admin']['time']);
					$_SESSION['admin']['ip']=$res['login_ip'];	//上次登陆的IP
					$_SESSION['admin']['new_ip']=$_SERVER['REMOTE_ADDR'];	//本次登陆IP
					$_SESSION['admin']['time']=$res['time'];
					$_SESSION['admin']['name']=$user;
					//登陆成功，保存相关数据
					$data['time']=time();
					$data['errornum']=0;
					$data['login_ip']=$_SERVER['REMOTE_ADDR'];
					$data['loginnum'] = array('exp','loginnum+1');	//登陆次数+1
					$login=$admin->where($where)->save($data);
					if($login){
						$this->success('登陆成功！','/'.GROUP_NAME.'/Index');
					}
				}
			}


		}
		
		function loginout(){
			if(isset($_SESSION)) {
				unset($_SESSION);
				session_destroy();
				$this->success('退出成功！','/'.GROUP_NAME.'/Login');
			}else {
				$this->error('已经登出！');
			}
		}
		
		Public function Verify(){
			import('ORG.Util.Image');
			Image::buildImageVerify();
		}


}